<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>药品管理</title>
		<link rel="stylesheet" href="../../../res/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../res/css/common/font-awesome.min.css" media="all">
		<link rel="stylesheet" href="../../../res/layui-config/css/app.css" media="all">
	</head>
	<body>
		<div class="kit-table">
		    <form class="layui-form" lay-filter="kit-search-form">
				<!-- 左上角的两个按钮 -->
		        <div class="kit-table-header">
		            <div class="kit-search-inputs">
		                <div class="kit-search-keyword">
		                    <input type="text" class="layui-input" id="serach-one" placeholder="药品.." onKeyUp="lockInput(this,'serach-two')"/>
		                    <button lay-submit lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
		                </div>
		                <div class="kit-search-more" id="kit-search-more">更多筛选<i class="layui-icon">&#xe61a;</i></div>
		            </div>
		        </div>
				<!-- 搜索扩展工具条 -->
		        <div class="kit-search-mored layui-anim layui-anim-upbit">
		            <div class="kit-search-body">
		                <div class="layui-form-item">
		                    <label class="layui-form-label">药品名称</label>
		                    <div class="layui-input-block">
		                        <input type="text" name="drugName" id="serach-two" placeholder="搜索关键字.." autocomplete="off" class="layui-input" onKeyUp="lockInput(this,'serach-one')">
		                    </div>
		                </div>
		                <div class="layui-form-item">
			                <label class="layui-form-label">类型</label>
			                <div class="layui-input-block">
			                    <select name="dtId" id="drug-type">
			                      <option value=""></option>
			                    </select>
			                </div>
		                </div>
		                <div class="layui-form-item">
		                    <div class="layui-inline">
		                        <label class="layui-form-label">范围</label>
		                        <div class="layui-input-block">
			                        <div class="layui-input-inline" style="width: 100px;">
			                            <input type="text" name="priceMin" placeholder="￥" autocomplete="off" class="layui-input" maxlength="6" onKeyUp="amount(this)">
			                        </div>
			                        <div class="layui-form-mid">-</div>
			                        <div class="layui-input-inline" style="width: 100px;">
			                            <input type="text" name="priceMax" placeholder="￥" autocomplete="off" class="layui-input" maxlength="6" onKeyUp="amount(this)">
			                        </div>
		                        </div>
		                    </div>
		                </div>
		            </div>
		            <div class="kit-search-footer">
		                <button type="reset" class="layui-btn layui-btn-sm layui-btn-primary kit-btn reset-btn">重置</button>
		                <button lay-submit lay-filter="search" class="layui-btn layui-btn-sm kit-btn">确定</button>
		            </div>
		        </div>
		    </form>
			<script type="text/html" id="aduit-detail-tpl">
		    <div style="margin:10px">
		        <form class="layui-form" action="" lay-filter="form-edit">
					<div class="layui-form-item" style="padding: 8px 0px 0px 8px;color: #FF5722;">
		                <h3>总经理审核</h3>
		            </div>
		            <input type="hidden" name="audId" value="{{d.rowdata.audId}}" />
					<div class="layui-form-item">
		                <label class="layui-form-label">审核状态：</label>
		                <div class="layui-input-block" style="line-height: 35px;padding-left: 8px;">
							<input type="radio" name="audState" value="10111" title="通过" />
		                    <input type="radio" name="audState" value="10110" title="不通过" checked/>
		                </div>
		            </div>
					<div class="layui-form-item">
		                <label class="layui-form-label">审核意见：</label>
		                <div class="layui-input-block" style="line-height: 35px;padding-left: 8px;">
							<textarea name="audIdea" required lay-verify="required" placeholder="意见" autocomplete="off" class="layui-textarea"></textarea>
		                </div>
		            </div>
					<div class="layui-form-item">
		                <div class="layui-input-block" style="padding-left: 8px;">
		                    <input type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="formEdit" style="padding: 0px 20px" value='提交'>
							<button type="reset" class="layui-btn layui-btn-sm layui-btn-primary">重置</button>
		                </div>
		            </div>
		        </form>
		    </div>
			</script>
			<!-- 操作按钮 -->
		    <div class="kit-table-body">
		        <table id="demo" lay-filter="demo"></table>
		        <script type="text/html" id="views">
					<button class="layui-btn layui-btn-xs" lay-event="specification">说明书</button>
					<button class="layui-btn layui-btn-xs" lay-event="matDetail">物料明细</button>
					<button class="layui-btn layui-btn-xs" lay-event="aduitDetail">审核明细</button>
		        </script>
		        <script type="text/html" id="tools">
					<button class="layui-btn layui-btn-xs" lay-event="audit">审核</button>
		        </script>
		    </div>
		</div>
		<script src="../../../res/js/common/jquery-1.11.2.min.js"></script>
		<script>
			//文本框只能输入金额
		    function amount(th){
		        var regStrs = [
		            ['^0(\\d+)$', '$1'], //禁止录入整数部分两位以上，但首位为0
		            ['[^\\d\\.]+$', ''], //禁止录入任何非数字和点
		            ['\\.(\\d?)\\.+', '.$1'], //禁止录入两个以上的点
		            ['^(\\d+\\.\\d{2}).+', '$1'] //禁止录入小数点后两位以上
		        ];
		        for(i=0; i<regStrs.length; i++){
		            var reg = new RegExp(regStrs[i][0]);
		            th.value = th.value.replace(reg, regStrs[i][1]);
		        }
		    }
			//搜索框同步输入
			function lockInput(obj,id){
				$("#"+id).val($(obj).val());
			}
		</script>
		<script src="../../../res/layui/layui.all.js"></script>
		<script type="text/html" id="img">
			<img style="height:100%;height:30px;" onclick="getPathOver(this)" src="../../../{{ d.drugPictrue}}">
		</script>
		<script>
			//弹出层下标
			var layerIndex1,layerIndex2,layerIndex3,editIndex1,editIndex2;
			var admin = getSession();
			var adminName = admin==null ? null : admin.adminName;
			
			//显示表格数据
		    layui.use(['table','upload'], function() {
		        var table = layui.table,
		            $ = layui.jquery,
		            layer = layui.layer,
		            form = layui.form,
		            laytpl = layui.laytpl;
		        var tableIns = table.render({
		            elem: '#demo',
		            height: '449px', //容器高度
		            url: '../../../drug/showAudit.action',
		            page: true,
		            id: 'demo',
		            cols: [
		                [{checkbox: true,fixed: true},
		                {field: 'drugName',title: '药名'},
		                {field: 'dtName',title: '类型',width: 80},
		                {field: 'drugUnit',title: '单位',width: 80},
		                {field: 'drugProp',title: '属性',width: 90},
		                {field: 'drugPictrue',title: '图片',width: 60,templet:'#img'},
		                {field: 'drugPrice',title: '价格（元）',width: 120,sort: true},
		                {field: 'audState',title: '审核状态',width: 120},
		                {fixed: 'right',title: '查看',width: 240,align: 'center',toolbar: '#views'},
		                {fixed: 'right',title: '操作',width: 100,align: 'center',toolbar: '#tools'}]
		            ],
		            done: function(res, curr, count) {
		            	var tableElem = this.elem.next('.layui-table-view');
		                $("[data-field = 'drugProp']").children().not(":first").each(function(){
						    if($(this).text() == '1'){
						    	$(this).text("处方药");
						    } else if ($(this).text() == '0') {
						    	$(this).text("非处方药");
						    }
					  	});
		                $("[data-field = 'drugPictrue']").children().not(":first").each(function(){
		                	var obj = $(this);
		                	obj.find('img').on("error", function () {
		                		obj.text('无');
		                	});
		                	obj.find('img').on("load", function () {
		                		obj.find('img').mouseover(function() {
			                		layer.tips("点击查看", this, {
		                            	tips: [3, "#4794ec"],time: 1000
		                          	});
		                      	});
		                	});
					  	});
		                $("[data-field = 'audState']").children().not(":first").each(function(index){
						    if($(this).text() == '10101'){
						    	$(this).text("未审核");
						    } else {
						    	$(this).text("审核完成");
						    	tableElem.find('tr[data-index="' + index + '"]').find('button[lay-event="specification"],button[lay-event="matDetail"],button[lay-event="audit"]').addClass('layui-btn-disabled').attr('disabled',true);
						    }
					  	});
		            }
		        });
		        
		      	//点击排序重新渲染表格
		        table.on('sort(demo)', function(obj){
		        	tableIns.reload();
		        });
		        
				//查询类型下拉框数据
		        var staticData = null;
		        $.ajax({ url:"../../../drugType/showList.action",
                    type:'post',//method请求方式，get或者post
                    async:false,//同步
                    dataType:'json',//预期服务器返回的数据类型
                    success:function(mes){//res为相应体,function为回调函数
                    	//增加及修改类型下拉框内的数据
                    	staticData = mes;
                    	//循环添加类型搜索框内的数据
                    	$.each(mes.types,function(index,item){
                    		$("#drug-type").append("<option value="+item.dtId+">"+item.dtName+"</option>");
                    	});
                    	//渲染类型搜索框内的下拉框
                    	renderForm();
                    }
                 });
		        
		        //监听搜索表单提交
		        form.on('submit(search)', function(data) {  
		            //带条件查询
		            tableIns.reload({
		                where: data.field,
		                page: { curr: 1 }
		            });
		            $('.kit-search-mored').hide();//关闭搜索层
		            return false;
		        });
		        
		        //监听工具条按钮点击
		        table.on('tool(demo)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		            var data = obj.data; //获得当前行数据
		            var layEvent = obj.event; //获得 lay-event 对应的值
		            var tr = obj.tr; //获得当前行 tr 的DOM对象
		
		            if (layEvent === 'matDetail') { //物料清单
		            	layer1(data.drugId,data.drugName);
		            } else if (layEvent === 'specification') { //说明书
		            	layer2(data.drugId,data.drugName);
		            } else if (layEvent === 'audit') { //审核
		            	var d = {
		                	rowdata: {
		                		audId: data.audId,bln: true
	                        }
		                };
			            editLayer1(d);
		            } else if (layEvent === 'aduitDetail') {
		            	layer3(data.drugId,"0","总经理审核");
		            }
		        });
		        
		        //表单渲染
		        form.render(null, 'kit-search-form');
		        //更多筛选点击事件
		        $('#kit-search-more').on('click', function() {
		            $('.kit-search-mored').toggle();
		        });
		        
		      	//审核弹出层   提交按钮点击事件
		        form.on('submit(formEdit)', function(data) {
		        	data.field["audName"]=adminName;
		        	update(data.field);
                	return false;
		        });
		        
		    });
			
		 	//渲染 生产计划审核和审核明细弹出层
		    function editLayer1(d){
	    		layui.laytpl($('#aduit-detail-tpl').html()).render(d,function(html) {
	    			editIndex1 = layer.open({
                        type: 1,
                        title: '经理审核明细',
                        content: html,
                        maxmin: true,
                        area: ['600px', '380px'],
                        success: function() {
                            layui.form.render(null, 'form-edit');
                        }
                    });
                });
	    		return 
		    }
			
		  	//显示 物料清单
			function layer1(id,name){
				layerIndex1 = layer.open({
				  type: 2,
				  title: '药品配置',
				  content: '../../materialList/details/index.html?id='+id+'&name='+name,
				  area: ['800px', '430px'],
				  maxmin: true
				});
				layer.full(layerIndex1);
			}
		  	
		  	//显示 说明书
			function layer2(id,name){
				layerIndex2 = layer.open({
				  type: 2,
				  title: '说明书',
				  content: '../../specification/specification.html?id='+id+'&name='+name,
				  area: ['800px', '430px'],
				  maxmin: true,
				  success: function (layero, index)
                  {
                      var body = layer.getChildFrame('body', index);
                      var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                      body.find("#btn,#btn-err").remove();
                  }
				});
		  	}
		  	
			//显示 审核明细
			function layer3(id,type,name){
				layerIndex3 = layer.open({
				  type: 2,
				  title: '审核明细',
				  content: '../detail/drugDetail.html?id='+id+'&type='+type+'&name='+name,
				  area: ['800px', '430px'],
				  maxmin: true
				});
		  	}
		  	
			//进行审核
		    function update(data){
		    	$.ajax({
		    		url: "../../../audit/addOrUpdate.action",
                    type:'post',//method请求方式，get或者post
                    async:false,//同步
                    dataType:'json',//预期服务器返回的数据类型
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data),//表格数据序列化
                    success:function(mes){//res为相应体,function为回调函数
                    	if(mes.status==1){ 
                        	editIndex1 && layer.close(editIndex1); //关闭弹出层
	                        $("#res").click();//调用重置按钮将表单数据清空 
	                        $(".layui-laypage-btn")[0].click();//当前表格刷新
                        }
                        layer.msg(mes.msg);
                    },
                   	error:function(){layer.alert('数据异常，请稍后重试！！！',{icon:5});}
                 });
		    }
		  	
		    //刷新弹出层
		    function layerIFrameReload(layerIndex) {
	    	  window['layui-layer-iframe' + layerIndex].location.reload();
	    	}
		    
		  	//重新渲染表单
		  	function renderForm(){ 
	  			layui.use('form', function(){ 
		  			var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加() 
		  			form.render(); 
	  			}); 
	  		}
		  	
		  	/**
			*鼠标进入时
			*/
			function getPathOver(obj){
				$("body").append('<div id="layer"></div><img id="pic" src="#" style="width:280px;height:280px;display:none;z-index:99;border-radius: 2px;margin: auto;position: absolute;top: 0px;bottom: 0px;left: 0px;right: 0px;border: 1px solid #424242"/>');
				$("#layer").addClass("layer").fadeIn("slow");
				$("#layer").click(function(){
					getPathOut();
				});
				$("#pic").fadeIn("slow");
				$('#pic').attr({"src":obj.src});
			}
			/**
			*鼠标退出时
			*/
			function getPathOut(){
				$("#layer").fadeOut("slow");
				$("#pic").fadeOut("slow");
			}
			
			/**
			*获取session
			*/
			function getSession(){
				var admin = null;
				$.ajax({ 
		        	url:"../../../admin/getSession.action",
	                type:'post',
	                async:false,
	                dataType:'json',
	                success:function(mes){
	                	if (mes!=null && mes!="") {
	                		admin = mes;
						}
	                }
	        	});
				return admin;
			}
		  	
		    //ajax加载动画
		    $(function () {
	            $.ajaxSetup({
	                layerIndex:-1,
	                beforeSend: function () {
	                    this.layerIndex = layer.load(1, { shade: [0.8, '#ffffff'] });
	                },
	                complete: function () {
	                    layer.close(this.layerIndex);
	                },
	                error: function () {
	                    layer.alert('部分数据加载失败，可能会导致页面显示异常，请刷新后重试', {
	                        skin: 'layui-layer-molv'
	                       , closeBtn: 0
	                       , shift: 4 //动画类型
	                    });
	                }
	            });
	        });
		</script>
	</body>
</html>
